package org.lhh.server.controller;

import org.apache.commons.codec.binary.Base64;
import org.lhh.server.common.api.JSONResult;
import org.lhh.server.config.init.KeyPairInit;
import org.lhh.server.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 辉仔
 * @since 2019-05-28
 */
@RequestMapping("/auth")
@RestController
public class AuthController {

    @Autowired
    private IUserService userService;

    @GetMapping("/getPublicKey")
    public JSONResult getPublicKey() {
        return JSONResult.ok(new String(Base64.encodeBase64(KeyPairInit.keyPair.getPublic().getEncoded())));
    }


    @PostMapping("/login")
    public JSONResult authLogin(String username, String password) {
        return userService.authLogin(username, password);
    }

    @GetMapping("/user/{id}")
    public JSONResult authUser(@PathVariable String id) {
        return JSONResult.ok(userService.authUser(id));
    }

}
